
import 'package:dio/dio.dart';

import '../../util/HDNavigator.dart';
import '../../util/LoadingUtils.dart';
import '../../util/string_util.dart';
import 'ResponseData.dart';

class HttpUtil {
  ///定义错误编码对应的规则
  static const List<Map<String, dynamic>> REQUEST_FAILED_TYPE = [
    {"code": 000, "msg": "token失效，请重新登录"},
    {"code": 400, "msg": "参数有误"},
    {"code": 402, "msg": "这是一个非法请求"},
    {"code": 403, "msg": "服务器拒绝请求"},
    {"code": 404, "msg": "访问地址不存在"},
    {"code": 404, "msg": "请求方式错误"},
    {"code": 500, "msg": "服务器开小差，请联系管理员"},
    {"code": 502, "msg": "无效的请求，请联系管理员"},
    {"code": 503, "msg": "未连接到服务器，\n请检查您的网络设置"}
  ];

  ///表示统一处理系统业务信息
  static BusinessDeal(Response response, ResponseData responseData) {
    dynamic status = responseData.code ?? 0;
    responseData.isok = false;
    switch (status.toString()) {
      case "0":
      case "6":
      case "3200": //请求正常
        responseData.isok = true;
        //这个地方可以对返回的数据做处理,重新给 responseData.data 赋值
        //response.data=responseData;
        //表示成功
        break;
      case "000": //请求st失效，重新登录
        LoadingUtils.showInfo("操作超时失效，请重新登录");
        //此时清空所有的路由，进入登录页面
        HDNavigator.logout();
        break;
      case "1503": //服务器正在升级中，预计升级时间2小时，请稍候再试
        LoadingUtils.showInfo("服务器正在升级中，预计升级时间2小时，请稍候再试");
        //此时清空所有的路由，进入登录页面
        HDNavigator.logout();
        break;
      case "403": //表示没有权限
        LoadingUtils.showInfo("没有权限,请联系管理员!");
        break;
      case "user-01":
      case "user-02":
      case "user-03":
      case "user-04":
      case "user-05":
      case "user-06":
      case "user-07":
      case "user-08":
      case "user-09":
      case "user-10":
      case "user-11":
        LoadingUtils.showNetWorkInfo("${responseData.msg}");
        break;
      case "BASE_000001":
      case "BASE_100001":
        LoadingUtils.showNetWorkInfo(
            responseData.msg ?? "服务端处理异常【${status.toString()}】,请联系管理员!");
        break;
      case "1":
        LoadingUtils.showNetWorkInfo(
            responseData.msg ?? "服务端处理异常【${status.toString()}】,请联系管理员!");
        break;
      default: // 可以扩展其他的错误编码
        LoadingUtils.showNetWorkInfo("${responseData.msg}");
    }
  }

  /// [hideNetError] hideNetError为true不显示错误信息提示
  static DealNetworkError(
      Exception error, bool hideLoading, bool hideNetError) async {
    String msg = "";
    if (error is DioError) {
      switch (error.type) {
        case DioErrorType.connectTimeout:
        case DioErrorType.receiveTimeout:
        case DioErrorType.sendTimeout:
          msg = '网络开小差,请求超时!';
          break;
        case DioErrorType.cancel:
          msg = "取消请求";
          break;
        case DioErrorType.response:
          // 先取response中的message
          var data = error.response?.data;
          if (data != null && data is Map) {
            msg = data["msg"] ?? "";
          }

          if (msg.isEmpty) {
            int? errCode = error.response?.statusCode;
            for (var element in HttpUtil.REQUEST_FAILED_TYPE) {
              if (element["code"] == errCode) {
                msg = element["msg"] ?? "";
                break;
              }
            }
          }

          if (StringUtil.isEmpty(msg)) {
            msg = "[${error.response?.statusCode.toString()}]网络异常";
          }
          break;
        case DioErrorType.other:
          msg = error.message;
          break;
        default:
          msg = "网络异常";
          break;
      }
    } else {
      msg = error.toString();
    }

    if (hideLoading) {
      LoadingUtils.dismiss();
    }

    //hideNetError为true不显示错误信息提示
    if (!hideNetError) {
      LoadingUtils.showNetWorkInfo(msg);
    }
    return msg;
  }
}
